#include "tcp_socket.hpp"

int main()
{
    TcpSocket lst_sock;
    //1. 创建套接字
    CHECK_RET(lst_sock.Socket());
    //2. 绑定地址信息
    CHECK_RET(lst_sock.Bind("192.168.197.128",3090));
    //3. 开始监听
    CHECK_RET(lst_sock.Listen());
    while(1){
        //4. 获取新建连接
        TcpSocket conn_sock;
        std::string client_ip;
        uint16_t client_port;
        bool ret = lst_sock.Accept(&conn_sock, &client_ip, &client_port);
        if (ret < 0) {
            continue;
        }
        std::cout << "new connect:" << client_ip << ":" << client_port << std::endl;
        //5. 通过新建连接与客户端通信
        std::string buf;
        ret = conn_sock.Recv(&buf);
        if (ret == false) {
            conn_sock.Close();
            continue;
        }
        std::cout << "client say: " << buf << std::endl;

        buf.clear();
        std::cout << "server say: ";
        fflush(stdout);
        std::cin >> buf;
        ret = conn_sock.Send(buf);
        if (ret == false) {
            conn_sock.Close();
            continue;
        }
    }
    //6. 关闭套接字
    lst_sock.Close();
    return 0;
}

